package edu.iupui.csci.mortgageplanner;

import edu.iupui.csci.mortgageplanner.util.Mortgage;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, TextWatcher{
	
	EditText propertyValueText;
	EditText downpaymentText;
	EditText interestRateText;
	EditText mortgageTermText;
	
	TextView monthlyPaymentValue;
	TextView mortgageInsuranceValue;
	TextView propertyTaxesValue;
	TextView homeInsuranceValue;
	TextView totalMonthlyPaymentValue;
	
	TextView totalInterestValue;
	TextView totalAmountValue;
	
	Button viewAmortizationButton;
	
	boolean requiresRecalculation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		propertyValueText = (EditText) findViewById(R.id.property_value_id);
		downpaymentText = (EditText) findViewById(R.id.downpayment_amount_id);
		interestRateText = (EditText) findViewById(R.id.interest_rate);
		mortgageTermText = (EditText) findViewById(R.id.default_mortgage_term_id);
		
		monthlyPaymentValue = (TextView) findViewById(R.id.monthly_payment_value_id);
		mortgageInsuranceValue = (TextView) findViewById(R.id.motgage_insurance_value_id);
		propertyTaxesValue = (TextView) findViewById(R.id.property_taxes_value_id);
		homeInsuranceValue = (TextView) findViewById(R.id.home_insurance_value_id);
		totalMonthlyPaymentValue = (TextView) findViewById(R.id.total_monthly_payment_value_id);
		
		totalInterestValue = (TextView) findViewById(R.id.total_estimated_interest_value_id);
		totalAmountValue = (TextView) findViewById(R.id.total_amount_id);
		
		viewAmortizationButton = (Button) findViewById(R.id.view_amortization_schedule_button_id);
		
		propertyValueText.addTextChangedListener(this);
		downpaymentText.addTextChangedListener(this);
		interestRateText.addTextChangedListener(this);
		mortgageTermText.addTextChangedListener(this);
		
		viewAmortizationButton.setOnClickListener(this);
		
		updateDisplay();
		requiresRecalculation = true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	  public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(this,DisplayAvailableRatesActivity.class);
		startActivity(intent);
	    return super.onOptionsItemSelected(item);
	  }

	@Override
	public void afterTextChanged(Editable s) {
		updateDisplay();
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClick(View arg0) {
		if(requiresRecalculation) {
			Mortgage.getMortgage().calculateMortgageDetails();
			requiresRecalculation = false;
		}
		Mortgage.getMortgage().getMortgageDetails();
		Intent intent = new Intent(this,AmortizationYearListActivity.class);
		intent.putExtra("mortgage_term", mortgageTermText.getText().toString());
		startActivity(intent);
	}
	
	protected void updateDisplay() {
		
		requiresRecalculation = true;
		String monthlyPayment = "Invalid Input";
		String monthlyTaxes = "Invalid Input";
		String monthlyInsurance = "Invalid Input";
		String monthlyPMI = "Invalid Input";
		String totalMonthlyPayment = "Invalid Input";
		
		String totalInterest = "Invalid Input";
		String totalAmount = "Invalid Input";
		
		Mortgage m = null;
		try {
			m = Mortgage.getMortgage(Double.parseDouble(propertyValueText.getText().toString()),
					Double.parseDouble(downpaymentText.getText().toString()),
					Double.parseDouble(interestRateText.getText().toString()),
					Double.parseDouble(mortgageTermText.getText().toString()));

			monthlyPayment = String.format("%1$,.2f", m.getMonthlyPayment());
			monthlyTaxes = String.format("%1$,.2f", m.getMonthlyTaxes());
			monthlyInsurance = String.format("%1$,.2f", m.getMonthlyInsurance());
			monthlyPMI = String.format("%1$,.2f", m.getMonthlyPMI());
			totalMonthlyPayment = String.format("%1$,.2f",  m.getMonthlyPayment() + m.getMonthlyTaxes() + m.getMonthlyInsurance() + m.getMonthlyPMI());
			
			totalInterest = String.format("%1$,.2f", m.getTotalInterest());
			totalAmount = String.format("%1$,.2f", m.getTotalAmount());

			
		} catch (NumberFormatException ex) {
			//DO Nothing
		} catch (Exception e) {
			//DO Nothing
		}

		monthlyPaymentValue.setText(monthlyPayment);
		mortgageInsuranceValue.setText(monthlyPMI);
		propertyTaxesValue.setText(monthlyTaxes);
		homeInsuranceValue.setText(monthlyInsurance);
		totalMonthlyPaymentValue.setText(totalMonthlyPayment);
		
		totalInterestValue.setText(totalInterest);
		totalAmountValue.setText(totalAmount);
	}

}
